// @file main.c
//
//  date Created on: June 22, 2020
//  author meiguoqiang@inspur.com
//
//  Description:
//
//
//

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>


#include "svdpi.h"
#include <iostream>
#include <math.h>
#include <vector>

#include "engine.h"


extern "C"  int  gen_wave(svBitVecVal fftin_re[64],svBitVecVal fftin_im[64]) 
{
	printf("gen_wave run begin!\n");
	Engine *ep;
	mxArray *GREAL = NULL, *GIMAG = NULL;

	double  re[64];
	double  im[64];
		
    	/*
	 * Call engOpen with a NULL string. This starts a MATLAB process 
     * on the current host using the command "matlab".
	 */
	if (!(ep = engOpen(""))) {
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		return EXIT_FAILURE;
	}

	
    	/*
	 * Evaluate a function of time, distance = (1/2)g.*t.^2
	 * (g is the acceleration due to gravity)
	 */
	engEvalString(ep, "cd ../model");
	engEvalString(ep, "addpath(genpath('./'))");
	engEvalString(ep, "[GREAL GIMAG]=gen_wave()");
	engEvalString(ep, "rdata=GREAL.data");
	engEvalString(ep, "idata=GIMAG.data");

	/*
	 * Get the variable result from the MATLAB workspace
	 */
	GREAL=engGetVariable(ep, "rdata");
	GIMAG=engGetVariable(ep, "idata");
        memcpy((void*)im, (void*)mxGetPr(GIMAG), sizeof(im));
        memcpy((void*)re, (void*)mxGetPr(GREAL), sizeof(re));

	//printf("Hit return to continue\n\n");
	//	fgetc(stdin);
	
	for(int i=0;i<64;i++) {
		printf("gen_wave[%d] is  %f  %f \n",i, re[i] , im[i]);
		fftin_re[i] = (int) (re[i]*8192);//2^13;
		fftin_im[i] = (int) (im[i]*8192);//2^13;
		printf("gen_wave[%d] is  %f  %f ---- %d  %d  \n",i, re[i] , im[i]  ,  fftin_re[i]   ,  fftin_im[i]);		
	}


	printf("gen_wave run end ! \n");
	engClose(ep); //关闭Matlab引擎，退出Matlab
	return 0 ;
}

//
//int main(void)
//{
//	int re[64];
//	int im[64];
//    		
//    gen_wave(re,im);
//
//    return 0;
//}
